// pages/person/phonePwd/phonePwd.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputForm: {
      newPwd: '',
      phone: '',
      authCode: '',
    },
    confirmPwd: '',
    isSend: true,
    time: 120,
    timer: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onUnload() {
    clearTimeout(this.data.timer)
  },
  onShow() {
    wx.hideHomeButton()
  },
  startInterval() {
    this.data.timer = setTimeout(() => {
      if (this.data.time <= 0) {
        this.setData({
          isSend: true,
          time: 120,
        })
        return
      }
      this.setData({
        time: this.data.time - 1,
      })
      this.startInterval()
    }, 1000)
  },

  getVerificationCode() {
    console.log('获取验证码')
    let { phone, newPwd } = this.data.inputForm
    let reg = /^(?:(?:\+|00)86)?1\d{10}$/
    if (!reg.test(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
      })
      return
    }
    wx.showLoading({ title: '加载中...' })
    wx.$API['home/getAuthCode'](this.data.inputForm)
      .then((res) => {
        console.log(res, '获取成功')
        wx.hideLoading()
        this.setData({
          isSend: false,
        })
        this.startInterval()
        wx.showModal({
          title: '提示',
          content: '验证码发送成功',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
            }
          },
        })
      })
      .catch((error) => {
        wx.hideLoading()
        console.log(error, 'error')
        wx.showToast({
          title: error,
          icon: 'none',
        })
      })
  },
  codeChange(e) {
    this.setData({
      'inputForm.authCode': e.detail.detail.value,
    })
  },
  confirmPwdChange(e) {
    this.setData({
      confirmPwd: e.detail.detail.value,
    })
  },
  phoneChange(e) {
    this.setData({
      'inputForm.phone': e.detail.detail.value,
    })
  },
  inputChange(e) {
    this.setData({
      'inputForm.newPwd': e.detail.detail.value,
    })
  },
  submit() {
    let { newPwd, authCode, phone } = this.data.inputForm
    if (!newPwd || !authCode || !phone) {
      wx.showToast({
        title: '请填全参数',
        icon: 'none',
      })
      return
    }
    if (newPwd !== this.data.confirmPwd) {
      wx.showToast({
        title: '两次输入密码不一致',
        icon: 'none',
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    wx.$API['home/changePwdPhone'](this.data.inputForm)
      .then((res) => {
        wx.hideLoading()
        let obj = wx.getStorageSync('changePwdStatus') || {}
        wx.setStorageSync('changePwdStatus', {
          ...obj,
          isSkipChangePwd: false,
        })
        wx.showModal({
          title: '提示',
          content: '密码修改成功',
          success(res) {
            wx.switchTab({
              url: '/pages/home/index',
            })
          },
        })
      })
      .catch((error) => {
        wx.hideLoading()
        console.log(error, 'error')
        wx.showModal({
          title: '提示',
          content: error,
          success(res) {},
        })
      })
  },
})
